package org.vincen.helloworld.rubbish;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 查询mysql当前schema有多少表
 * @author vincen
 */
public class R20111129 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cookbook", "cbuser", "cbpass");
			conn.setAutoCommit(false);
			Statement stat = conn.createStatement();
//			ResultSet rs = stat.executeQuery("show tables");
			ResultSet rs = stat.executeQuery("USE information_schema;SELECT column_name, column_type FROM columns WHERE table_name = 'limbs';");
			conn.commit();
			ResultSetMetaData meta = rs.getMetaData();
			int cc = meta.getColumnCount();
			for(int i = 1; i < cc; ++i) {
				if(i > 1)System.out.println(", ");
				System.out.println(meta.getColumnLabel(i));
			}
			System.out.println();
			while(rs.next()) {
				for(int i = 1; i <= cc; ++i) {
					if(i > 1)System.out.println(", ");
					System.out.println(rs.getString(i));
				}
				System.out.println();
			}
			rs.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
